package com.chmingx.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chmingx.edu.entity.Teacher;
import com.chmingx.edu.service.TeacherService;
import com.chmingx.edu.vo.TeacherQuery;
import com.chmingx.utils.CommonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author chmingx
 * @since 2021-09-20
 */
@Api("讲师管理")
@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有讲师数据
     *
     * @return List<Teacher>
     */
    @ApiOperation(value = "查询所有讲师数据")
    @GetMapping("/all")
    public CommonResponse<List<Teacher>> getAllTeacher() {
        CommonResponse<List<Teacher>> response = CommonResponse.ok();
        response.setData(teacherService.list(null));
        return response;
    }

    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("/{id}")
    public CommonResponse<Teacher> getTeacherById(@PathVariable("id") String id) {
        int a = 10 / 0;
        CommonResponse<Teacher> response = CommonResponse.ok();
        response.setData(teacherService.getById(id));
        return response;
    }

    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("/{id}")
    public CommonResponse<Boolean> deleteById(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable("id") String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            CommonResponse<Boolean> response = CommonResponse.ok();
            response.setData(flag);
            return response;
        } else {
            CommonResponse<Boolean> response = CommonResponse.error();
            response.setData(flag);
            return response;
        }

    }

    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/all/{currentPage}/{pageSize}")
    public CommonResponse<Page<Teacher>> pageTeacher(@PathVariable("currentPage") Long currentPage,
                                                     @PathVariable("pageSize") Long pageSize) {
        // 创建page对象
        Page<Teacher> teacherPage = new Page<>(currentPage, pageSize);
        // 调用方法实现分页, 会将所有分页数据封装到teacherPage对象中
        teacherService.page(teacherPage, null);

        CommonResponse<Page<Teacher>> response = new CommonResponse<>();
        response.setData(teacherPage);
        return response;
    }

    @ApiOperation(value = "条件查询讲师")
    @PostMapping("/query") // @RequestBody使用json传递数据，将json封装到对应对象里
    public CommonResponse<Page<Teacher>> queryTeacher(@RequestBody TeacherQuery teacherQuery) {
        CommonResponse<Page<Teacher>> response = new CommonResponse<>();
        response.setData(teacherService.query(teacherQuery));
        return response;
    }

    /**
     * 添加讲师
     *
     * @param teacher
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("/")
    public CommonResponse<Boolean> addTeacher(@RequestBody Teacher teacher) {
        boolean flag = teacherService.save(teacher);
        if (flag) {
            return CommonResponse.ok();
        } else {
            return CommonResponse.error();
        }
    }

    /**
     * 更新讲师
     * @param teacher
     * @return
     */
    @ApiOperation(value = "更新讲师")
    @PutMapping("/")
    public CommonResponse<Boolean> updateTeacher(@RequestBody Teacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        if (flag) {
            return CommonResponse.ok();
        } else {
            return CommonResponse.error();
        }
    }
}

